package tpdisenio2009.mvp.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import tpdisenio2009.Tpdisenio2009App;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.SoccerPlayer;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.mvp.display.QueryFantasyTeamView;
import tpdisenio2009.mvp.display.QueryPlayerScoresViewImpl;
import tpdisenio2009.mvp.display.UpdateFantasyTeamViewImpl2;
import tpdisenio2009.services.FantasyTeamPlayerService;
import tpdisenio2009.services.FantasyTeamService;
import tpdisenio2009.services.TournamentDateService;

/**
 *
 * @author DeuterioX
 */
public class QueryFantasyTeamPresenter extends DefaultPresenter<QueryFantasyTeamView>{

    public final FantasyTeamService fantasyTeamService;
    public final FantasyTeamPlayerService fantasyTeamPlayerService;
    public final TournamentDateService tournamentDateService;

    public ActionListener makeChangesListener;
    public ActionListener makeTransfersListener;
    public ActionListener viewPointsByDateListener;

    public QueryFantasyTeamPresenter( QueryFantasyTeamView view ) {
        super( view );

        fantasyTeamService = new FantasyTeamService();
        fantasyTeamPlayerService = new FantasyTeamPlayerService();
        tournamentDateService = new TournamentDateService();

        initialize();

        bind();
    };

    public void initialize() {
        User user = (User) Tpdisenio2009App.loggedUser;
        TournamentDate date = tournamentDateService.getFirstOpenTournamentDate();

        getView().loadFantasyTeam( fantasyTeamPlayerService.getFantasyTeamPlayersByDate(user.getFantasyTeam(), date) );
        getView().setFantasyTeamName( user.getFantasyTeam().getName() );
        getView().setTotalTournamentScore( user.getFantasyTeam().getTotalScore() );        
        getView().setValuation( fantasyTeamService.getCotization(user) );
    }

    public void bind() {
        makeChangesListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                TournamentDate date = tournamentDateService.getFirstOpenTournamentDate();
                if (date.getBanDate() != null && date.getBanDate().before( new Date() )) {
                    getView().showError( "No es posible realizar modificaciones en el equipo durante el período de veda.", "Transferencia invalida..." );
                    return;
                }
                FantasyTeamPlayer player = getView().getSelectedPlayer();
                
                if ( player == null ||  player.getRol() == null || ( player.getRol().equals(FantasyTeamPlayer.SUBSTITUTE_ROLE ) ) && ! player.getSoccerPlayer().getPosition().equals(SoccerPlayer.GOALKEEPER_POSITION) ) {
                    getView().showError( "Error en la substitución...", "Seleccione el jugador titular que desea reemplazar!" );
                    return;
                }

                FantasyTeamPlayer substitute;

                if( player.getSoccerPlayer().getPosition().equals(SoccerPlayer.GOALKEEPER_POSITION) ) {
                    if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                        substitute = getView().getPlayerByRoleString( player.getSoccerPlayer().getPosition() + " " + FantasyTeamPlayer.SUBSTITUTE_ROLE );
                    } else {
                        substitute = getView().getPlayerByRoleString( player.getSoccerPlayer().getPosition() + " " + FantasyTeamPlayer.TITULAR_ROLE );
                    }
                } else {
                    substitute = getView().getPlayerByRoleString( player.getSoccerPlayer().getPosition() + " " + FantasyTeamPlayer.SUBSTITUTE_ROLE );
                }
                                
                fantasyTeamPlayerService.updateSwapPlayers( player, substitute );

                User user = (User) Tpdisenio2009App.loggedUser;
                date = tournamentDateService.getFirstOpenTournamentDate();
                getView().loadFantasyTeam( fantasyTeamPlayerService.getFantasyTeamPlayersByDate(user.getFantasyTeam(), date) );
            }            
        };
        getView().addMakeChangesListener(makeChangesListener);

        makeTransfersListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdateFantasyTeamPresenter2 updateFantasyTeamPresenter = new UpdateFantasyTeamPresenter2( new UpdateFantasyTeamViewImpl2( null, true ) );
                updateFantasyTeamPresenter.getView().asWidget().setVisible( true );
                initialize();
            }
        };
        getView().addMakeTransfersListener(makeTransfersListener);

        viewPointsByDateListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                QueryPlayerScoresPresenter queryScoresPresenter = new QueryPlayerScoresPresenter( new QueryPlayerScoresViewImpl( null, true ) );
                queryScoresPresenter.getView().asWidget().setVisible( true );
            }
        };
        getView().addViewPointsByDateListener(viewPointsByDateListener);
    };

    public void unbind() {
        if (makeChangesListener != null)
            getView().removeMakeChangesListener( makeChangesListener );
        if (makeTransfersListener != null)
            getView().removeMakeTransfersListener( makeTransfersListener );
        if (viewPointsByDateListener != null)
            getView().addViewPointsByDateListener( viewPointsByDateListener );
    };
}
